#!/bin/bash

set -e
set -o pipefail

unset elf
unset spike
unset qemu
unset qemu_config
unset qemu_options
while [[ "$1" != "" ]]
do
    case "$1"
    in
    --elf)                   elf="$2";   shift 2;;
    --spike)               spike="$2";   shift 2;;
    --qemu)                 qemu="$2";   shift 2;;
    --qemu-config)   qemu_config="$2";   shift 2;;
    --qemu-options) qemu_options="$2";   shift 2;;
    *) echo "$0: Unknown argument $1";   exit 1;;
    esac
done

if [ "$elf" == "" ]
then
    echo "$0: --elf is required" >&2
    exit 1
fi
if [ "$qemu" == "" -a "$spike" == "" ]
then
    echo "$0: --qemu or --spike is required" >&2
    exit 1
else
    if [ "$qemu" != "" -a "$qemu_config" == "" ]
    then
        echo "$0: --qemu_config is required with --qemu option" >&2
        exit 1
    fi

    if [ ! -f $qemu_config ] ; then
        echo "$0: Unable to find QEMU config file ${qemu_config}" >&2
        exit 1
    fi
fi

if [ "$qemu" != "" ] ; then
    echo "Launching QEMU! Press Ctrl-A, X to exit"

    $qemu -readconfig $qemu_config -kernel $elf -nographic $qemu_options
elif [ "$spike" != "" ] ; then
    echo "Launching Spike!"

    $spike ${SPIKE_OPTIONS} $elf
fi

